package com.vijay.method.invoker;

import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.util.MethodInvoker;

import java.lang.reflect.InvocationTargetException;

public class MethodInvokerMain {

    public static void main(String[] args) throws Exception {

        invokeMethod();
        invokeStaticMethod();
        methodInvokingFactoryBean();
    }

    public static void methodInvokingFactoryBean() throws Exception {
        String staticMethod = "com.vijay.method.invoker.MyUtils.initialize";
        MethodInvokingFactoryBean fb = new MethodInvokingFactoryBean();
        //MethodInvokingFactoryBean is  MethodInvoker subclass
        fb.setStaticMethod(staticMethod);
        fb.setArguments("arg1", "arg2", "arg3");
        fb.afterPropertiesSet();
    }

    public static void invokeStaticMethod() throws Exception {
        String staticMethod = "com.vijay.method.invoker.MyUtils.initialize";
        MethodInvoker invoker = new MethodInvoker();
        invoker.setStaticMethod(staticMethod);
        invoker.setArguments("arg1", "arg2", "arg3");
        invoker.prepare();
        Object invoke = invoker.invoke();
        System.out.println(invoke);
    }

    private static void invokeMethod() throws Exception {
        String targetMethod = "onMessage";
        MethodInvoker invoker = new MethodInvoker();
        invoker.setTargetMethod(targetMethod);
        invoker.setTargetObject(new MessageHandler());
        invoker.setArguments("one  item  message come!");
        invoker.prepare();
        Object result = invoker.invoke();
        System.out.println("result = " + result);
    }


}
